<?php
function VoteData_adminform_main (){
  VoteData_adminform_submit ();
}

function VoteData_adminform_submit (){
    //$date_time = getdate();
    $forward =  FormUtil::getPassedValue ('forward', null);
    $ctrl =  FormUtil::getPassedValue ('ctrl', null);
    $form = FormUtil::getPassedValue ('form', null);
    $id = FormUtil::getPassedValue ('id', null);

    $is_array = FormUtil::getPassedValue ('array', false);
    if ($is_array){
      $is_array = true;
    }else{
      $is_array = false;
    }
    if (empty($ctrl)){
      if ($form[ctrl]){
        $ctrl = $form[ctrl];      
      }else{
        return 'ERROR VoteData system can not find controller variable';
      }
    }

    $form_url = pnModURL('VoteData', 'admin', 'form' , array('ctrl'=>$ctrl));


    //Forward to list page
    $list_url = pnModURL('VoteData', 'admin', 'list' , array('ctrl'=>$ctrl,
                                                                   'form[pro_id]' => $form['pro_id'],
                                                                   'form[dis_id]' => $form['dis_id'],
                                                                   'form[tam_id]' => $form['tam_id']
                                                        )
                        );

    if (isset($_POST['button_cancel']) || isset($_POST['button_cancel_x'])){
        pnRedirect($list_url);
        return true;
    }

    if (!($class = Loader::loadClassFromModule ('VoteData', $ctrl , $is_array))){
        return LogUtil::registerError ("Unable to load class [$ctrl] ...");
    }
    $object = new $class ();
    $object->getDataFromInput ('form',null,'POST');


    if ($_POST['button_delete'] || $_POST['button_delete_x']){
        $object->delete ();
    }else{
        $object->save ();
    }
    if (method_exists($object,'genForward')){
      $forwar_url = $object->genForward();
      if (!empty($forwar_url)){
        pnRedirect($forwar_url);
      }
    }else if (count($forward) > 0 && $forward[id]){
      $forwar_url = generateUrl($forward);
      pnRedirect($forwar_url);
    }else{
      pnRedirect($list_url);
    }
    
    return true;
}

function VoteData_adminform_doUpload(){
    $ctrl =  FormUtil::getPassedValue ('ctrl', null);
    $list_url = pnModURL('VoteData', 'admin', 'list' , array('ctrl'=>$ctrl));

    if ($_FILES["file"]["type"] == "text/ascii"){
      if ($_FILES["file"]["error"] > 0){
        pnRedirect($list_url);
      }else{
        $ext_file = strtolower(ereg_replace(".*\.(.*)$","\\1",$_FILES['file']['name']));
        //Directory path
        $path = "pnTemp/votedata_upload";
        $degis_upload = str_replace('\\', '/', $path );
        if (! is_dir($degis_upload)) {
            mkdir($degis_upload,0755,TRUE);
        }

        //Get next excel native id
        $where = ' ';
        $next_id   = DBUtil::selectFieldMax('votedata_migrate', 'id', 'MAX', $where)+1;

        //Gennerate file name
        $file       = "votedata_".$next_id."". time() .".".$ext_file;
        //var_dump($file); exit;
        //Get info of excel value to database
        $object = array(
                        'id'         => $next_id ,
                        'filepath'   => $degis_upload,
                        'filename'   => $file , 
                        'filenamedb' => $_FILES['file']['name'], 
                        'status'     => '0'   
                  );
        DBUtil::insertObject($object, 'votedata_migrate');
        move_uploaded_file($_FILES["file"]["tmp_name"], $degis_upload . '/' . $file);
      }
      pnRedirect($list_url);
    }else{
      pnRedirect($list_url);
    }
    
    return true;
}

/**
* @param $urlarray  the array to generate contain key and value
*                   ex.  array('modname'=>'VoteData'
                               'func'   =>'save'
                               'type'   =>'admin');
*/
function generateUrl($urlarray){
  //default value

  $forward =  FormUtil::getPassedValue ('forward', null);

  $modname = 'VoteData';

  if($forward['func']){
    $func = $forward['func'];
  }else{
    $func = 'list';
  }

  if($forward['id']){
    $param[] = 'id='.$forward['id'];
  }

  $type = 'admin';

  $param = array();
  foreach($urlarray as $key => $value){
    if (strcmp(strtolower($key),'modname') === 0){
      $modname = $value;
    }else if (strcmp(strtolower($key),'func') === 0){
      $func = $value;
    }else if (strcmp(strtolower($key),'type') === 0){
      $type = $value;
    }else{
      $param[] = $key . '=' .$value;
    }
  }
  return "index.php?module=$modname&func=$func&type=$type&" . implode('&',$param);
}

function VoteData_fixEncoding($in_str) {
  $cur_encoding = mb_detect_encoding($in_str) ;

  if($cur_encoding == "UTF-8" && mb_check_encoding($in_str,"UTF-8"))
    return $in_str;
  else
    return utf8_encode($in_str);
} 
?>